﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace biolit_config
{
    
    public partial class Biolit_Init_Dialog : Form
    {
        Ontologies m_Ontols = new Ontologies();
        List<OntList> m_SelectedList = new List<OntList>();
        public Biolit_Init_Dialog()
        {
            InitializeComponent();
        }

        private void tbOntology_Click(object sender, EventArgs e)
        {

        }

        public List<OntList> NewOntologies
        {
            get { return m_SelectedList; }
        }
        private void Biolit_Init_Dialog_Load(object sender, EventArgs e)
        {
            Application.EnableVisualStyles();
            LoadSettings();
        }


        private void LoadSettings()
        {
            Biolit_Settings.LoadSettings();
            ConfigSettings configSettings = Biolit_Settings.Settings;
            
            this.cbAlwaysOffline.Checked = configSettings.AlwaysOffline ;
            this.cbInitRun.Checked = configSettings.AutoStart;
            this.cbInlineMatch.Checked = configSettings.InlineMatching;
            this.cbNCBI.Checked = configSettings.NCBI;
            this.cbNoMatch.Checked = configSettings.NoMatchCommonTerms;
            this.cbPDB.Checked = configSettings.ProteinDataBank;
            this.cbUniprot.Checked = configSettings.UniProt;

            LoadOntSelectionFromSettings();


            this.lbSelectedOntologies.DataSource = m_SelectedList;
            this.lbSelectedOntologies.DisplayMember = "Name";

            this.lbOntologyList.DataSource = Biolit_Settings.Settings.AvailableOntologies;
            this.lbOntologyList.DisplayMember = "Name";



        }

        private void LoadOntSelectionFromSettings()
        {
            m_SelectedList.Clear();
            //if (Globals.ThisAddIn.m_SelectedOntologies != null) PFF
            //{
            //    foreach (OntList ontology in Globals.ThisAddIn.m_SelectedOntologies)
            //    {
            //        OntList Sel = new OntList();
            //        Sel.Name = ontology.Name;
            //        Sel.ID = ontology.ID;
            //        m_SelectedList.Add(Sel);
            //    }
            //}
        }


        void lbOntologyList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            DrawListItem(sender, e, Biolit_Settings.Settings.AvailableOntologies[e.Index].Name);

        }


        void lbOntologyList_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
        {
            e.ItemHeight = 35;
        }


        void SelectedlbOntologyList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            DrawListItem(sender, e, this.m_SelectedList[e.Index].Name);
        }


        void DrawListItem(object sender, System.Windows.Forms.DrawItemEventArgs e, string DrawString)
        {
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index,
                                          e.State ^ DrawItemState.Selected,
                                          e.ForeColor, Color.PapayaWhip);
            }

            e.DrawBackground();
            e.DrawFocusRectangle();
            //e.Graphics.DrawRectangle(new Pen(Color.Black), e.Bounds);
            FontFamily ff = new FontFamily("Calibri");
            Font f = new Font(ff, 14,FontStyle.Bold,GraphicsUnit.Pixel);
            e.Graphics.DrawString(DrawString, f, new SolidBrush(Color.Black), e.Bounds);
        }


        private void OnOntologySelected(object sender, EventArgs e)
        {
            OntList o = (OntList)((ListBox)sender).SelectedItem;
            SelectOntology(o);          
        }


        private void SelectOntology(OntList o)
        {

            OntList Sel = new OntList();
            Sel.Name = o.Name;
            Sel.ID = o.ID;
            Sel.Version = o.Version;
            Sel.Format = o.Format;
            foreach (var item in m_SelectedList)
            {
                if (item.ID == o.ID)
                {
                    MessageBox.Show(o.Name + " Ontology has already been Selected", "BioLit Error", MessageBoxButtons.OK);
                    return;
                }
            }
            m_SelectedList.Add(Sel);

            this.lbSelectedOntologies.DataSource = null;


            this.lbSelectedOntologies.DataSource = m_SelectedList;
            this.lbSelectedOntologies.DisplayMember = "Name";
        }

        private void OntList_OnMouseHover(object sender, EventArgs e)
        {
            //Ontology o = (Ontology)lbOntologyList.SelectedItem;
            //this.lbToolTip.UseFading = true;
            //this.lbToolTip.IsBalloon = true;
            //this.lbToolTip.SetToolTip(this.lbOntologyList, o.Name);
            
            
        }

        private void OnMouseClick(object sender, MouseEventArgs e)
        {
            //Ontology o = (Ontology)lbOntologyList.SelectedItem;
            //this.lbToolTip.UseFading = true;
            //this.lbToolTip.IsBalloon = true;
            //this.lbToolTip.SetToolTip(this.lbOntologyList, o.Name);
        }

        private void OnToolTipPopup(object sender, PopupEventArgs e)
        {
             
        }

        private void btnNext_Click(object sender, EventArgs e)
        {

            DownLoadProgress dlp = new DownLoadProgress();
            dlp.Downloads = this.m_SelectedList;
            dlp.Show();
            dlp.DoDownload();
            //if (this.btnNext.Text == "&Next")
            //{
            //    this.lbSelectedOntologies.Hide();
            //    this.lbOntologyList.DataSource = this.m_SelectedList;
            //    this.lbOntologyList.DisplayMember = "Name";
            //    this.btnFetchOnto.Show();
            //    this.btnUpdateOnto.Show();
            //    this.btnNext.Text = "&Back";
            //    this.label2.Hide();
            //    return;
            //}

            //if (this.btnNext.Text == "&Back")
            //{
            //    this.lbSelectedOntologies.DataSource = null;
            //    LoadOntSelectionFromSettings();
            //    this.lbSelectedOntologies.Show();

            //    this.lbSelectedOntologies.DataSource = this.m_SelectedList;
            //    this.lbSelectedOntologies.DisplayMember = "Name";

            //    this.lbOntologyList.DataSource = Biolit_Settings.Settings.AvailableOntologies;
            //    this.lbOntologyList.DisplayMember = "Name";
            //    this.btnFetchOnto.Hide();
            //    this.btnUpdateOnto.Hide();
            //    this.label2.Show();
            //    this.btnNext.Text = "&Next";
            //    return;
            //}

        }

        private void btnUpdateOnto_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // TODO USE CONFIG
            System.Diagnostics.Process.Start("http://www.codeplex.com/UCSDBioLit/Thread/View.aspx?ThreadId=44839");
        }

        
        
        private void btnFetchOnto_Click(object sender, EventArgs e)
        {
            DownLoadProgress dlp = new DownLoadProgress();            
            dlp.Downloads = this.m_SelectedList;
            dlp.Show();
            dlp.DoDownload();
            
                        
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // Need to store the settings 
            ConfigSettings cs = Biolit_Settings.Settings;
            cs.AlwaysOffline = this.cbAlwaysOffline.Checked;
            cs.AutoStart = this.cbInitRun.Checked;
            cs.InlineMatching = this.cbInlineMatch.Checked;
            cs.NCBI = this.cbNCBI.Checked;
            cs.NoMatchCommonTerms = this.cbNoMatch.Checked;
            cs.ProteinDataBank = this.cbPDB.Checked;
            cs.UniProt = this.cbUniprot.Checked;
            //cs.SelectedOntologies = this.m_SelectedList;
            

            
            Biolit_Settings.StoreSettings();

            this.Close();
            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OntList o =  (OntList)this.lbSelectedOntologies.SelectedItem;
            if (o != null)
            {
                m_SelectedList.Remove(o);
                this.lbSelectedOntologies.DataSource = null;      
                this.lbSelectedOntologies.DataSource = m_SelectedList;
                this.lbSelectedOntologies.DisplayMember = "Name";
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OntList o = (OntList)this.lbOntologyList.SelectedItem;
            if (o != null)
            {
                SelectOntology(o);
            }
        }
        
    }
}
